<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="dno">
      <el-input v-model="dataForm.dno" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="收货人" prop="consignee">
      <el-input v-model="dataForm.consignee" placeholder="收货人"></el-input>
    </el-form-item>
    <el-form-item label="支付状态" prop="status1">
      <el-input v-model="dataForm.status1" placeholder="支付状态"></el-input>
    </el-form-item>
    <el-form-item label="发货状态" prop="status2">
      <el-input v-model="dataForm.status2" placeholder="发货状态"></el-input>
    </el-form-item>
    <el-form-item label="打印" prop="print">
      <el-input v-model="dataForm.print" placeholder="打印"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="payment">
      <el-input v-model="dataForm.payment" placeholder="支付方式"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="uname">
      <el-input v-model="dataForm.uname" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dno: '',
          consignee: '',
          status1: '',
          status2: '',
          print: '',
          payment: '',
          uname: '',
          createTime: ''
        },
        dataRule: {
          dno: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          consignee: [
            { required: true, message: '收货人不能为空', trigger: 'blur' }
          ],
          status1: [
            { required: true, message: '支付状态不能为空', trigger: 'blur' }
          ],
          status2: [
            { required: true, message: '发货状态不能为空', trigger: 'blur' }
          ],
          print: [
            { required: true, message: '打印不能为空', trigger: 'blur' }
          ],
          payment: [
            { required: true, message: '支付方式不能为空', trigger: 'blur' }
          ],
          uname: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/ordersurface/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dno = data.orderSurface.dno
                this.dataForm.consignee = data.orderSurface.consignee
                this.dataForm.status1 = data.orderSurface.status1
                this.dataForm.status2 = data.orderSurface.status2
                this.dataForm.print = data.orderSurface.print
                this.dataForm.payment = data.orderSurface.payment
                this.dataForm.uname = data.orderSurface.uname
                this.dataForm.createTime = data.orderSurface.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/ordersurface/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dno': this.dataForm.dno,
                'consignee': this.dataForm.consignee,
                'status1': this.dataForm.status1,
                'status2': this.dataForm.status2,
                'print': this.dataForm.print,
                'payment': this.dataForm.payment,
                'uname': this.dataForm.uname,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
